package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.network.entity.ImInfoEntity;
import com.zw.yzk.kabu.im.ImUserUtil;
import com.zw.yzk.kabu.internal.component.DaggerImInfoComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.ImInfoModule;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.ImInfoPresenter;
import com.zw.yzk.kabu.view.view.ConversationView;

import javax.inject.Inject;

import butterknife.OnClick;


/**
 * Created by zhanwei on 2017/5/14.
 */

public class ConversationActivity extends BaseActivity implements ConversationView {

    @Inject
    ImInfoPresenter presenter;
    private String title;
    private int targetId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_conversation;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        setConversationInfo();
    }

    @Override
    public void initComponent() {
        DaggerImInfoComponent.builder()
                .applicationComponent(getApplicationComponent())
                .imInfoModule(new ImInfoModule())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        presenter.setVeiw(this);
    }

    @Override
    public void getImInfoSuccess(ImInfoEntity entity) {
        if (entity.result.isSeller == 1) {
            //设置导航栏
            ToolbarBuilder
                    .createBuilder(this)
                    .setTitle(title)
                    .setRightText(getResources().getString(R.string.go_to_store))
                    .build();
            targetId = entity.result.userId;
        }
    }

    @OnClick(R.id.right_text)
    public void startStoreActivity() {
        try {
            Intent intent = new Intent(this, StoreActivity.class);
            intent.putExtra(ActivityConstant.STORE_OWNER_ID, targetId);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置会话信息
     */
    private void setConversationInfo() {
        //设置页面信息
        Intent intent = getIntent();
        title = intent.getData().getQueryParameter("title");
        if (TextUtils.isEmpty(title)) {
            title = getResources().getString(R.string.conversation_detail);
        }
        //设置导航栏
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(title)
                .build();

        //查询聊天对象信息
        getImInfo(intent.getData().getQueryParameter("targetId"));
    }

    /**
     * 获取聊天对象信息
     *
     * @param userId 用户id
     */
    private void getImInfo(String userId) {
        presenter.getImInfo(userId);
    }

}
